//
//  XWMeViewController.m
//  CGWSWW
//
//  Created by lanou3g on 16/8/12.
//  Copyright © 2016年 CXG. All rights reserved.
//

#import "XWMeViewController.h"
#import "HeadHelp.h"
#import "XWSettingGroup.h"
#import "XWSettingArrowItem.h"
#import "XWSettingItem.h"
#import "XWSettingLabelItem.h"
#import "XWSettingSwitchItem.h"
#import "XWSettingViewController.h"
#import "XWCommonButton.h"
#import "XWLoginOrRegisterController.h"
#define  BackGroupHeight KScreen_Height*0.26
#define  HeadImageHeight  BackGroupHeight/2
#define CommonRatio 100/2
@interface XWMeViewController ()
{

    UIImageView *imageBG;
    UIView *BGView;
   
}
@property(nonatomic,strong)XWCommonButton *loginBtn;
@end

@implementation XWMeViewController
-(XWCommonButton*)loginBtn{
    if (!_loginBtn) {
        _loginBtn = [[XWCommonButton alloc]init];

        
    }
    return _loginBtn;
}
- (void)viewWillAppear:(BOOL)animated {
    [self.navigationController setNavigationBarHidden:YES animated:animated];
    [super viewWillAppear:animated];
    [self setupUI];
}

- (void)viewWillDisappear:(BOOL)animated {
    [self.navigationController setNavigationBarHidden:NO animated:animated];
    [super viewWillDisappear:animated];
}
-(void)setUpGroup00{
    XWSettingItem *settingItem = [XWSettingArrowItem itemWithIcon:@"" title:@"设置" destVcClass:[XWSettingViewController class]];
    XWSettingItem *nightItem = [XWSettingSwitchItem itemWithtemWithIcon:@"" title:@"夜间模式" Target:self action:@selector(nightItemAction:) forControlEvents:(UIControlEventValueChanged)];
    
    XWSettingLabelItem *cacheItem = [XWSettingLabelItem itemWithIcon:@"" title:@"清除缓存"];
#warning 清除缓存
    cacheItem.option = ^ {
        
    };
    cacheItem.accessoryLabelView_text = @"(0.0M)";
    XWSettingGroup *settingGroup00 = [[XWSettingGroup alloc]init];
    settingGroup00.items = @[settingItem,nightItem,cacheItem];
    [self.data addObject:settingGroup00];
    
}
-(void)setUpGroup01{
#warning 实现代码块
    XWSettingItem *intorItem = [XWSettingArrowItem itemWithIcon:@"" title:@"推荐给好友"];
    XWSettingItem *newVersionItem = [XWSettingArrowItem itemWithIcon:@"" title:@"最新版本"];
    XWSettingItem *prodFuncItem = [XWSettingArrowItem itemWithIcon:@"" title:@"产品新功能"];

    XWSettingGroup *settingGroup01 = [[XWSettingGroup alloc]init];
    settingGroup01.items = @[intorItem,newVersionItem,prodFuncItem];
    [self.data addObject:settingGroup01];

}
- (void)viewDidLoad {
    [super viewDidLoad];

    self.view.backgroundColor = XWCommonColor;
    //加载UI
    
//    self.automaticallyAdjustsScrollViewInsets=NO;//不适配自动布局
    [self setUpGroup00];
    [self setUpGroup01];
}
#pragma mark 夜间模式
-(void)nightItemAction:(UISwitch*)sender{
    XWLogFunc

}

-(void)setupUI
{
    //让头部视图和tableView一同出现 设置头部偏移量
    self.tableView.contentInset=UIEdgeInsetsMake(BackGroupHeight, 0, 0, 0);
    //先添加tableView的子视图 (头部视图背景视图)
    imageBG=[[UIImageView alloc]init];
    imageBG.frame=CGRectMake(0, -BackGroupHeight, KScreen_Width, BackGroupHeight);
    imageBG.image=[UIImage imageNamed:@"bguser"];
    
    [self.tableView addSubview:imageBG];
    //再添加tableView的子视图和头部背景视图同大小 去掉View的背景颜色
    BGView=[[UIView alloc]init];

    BGView.frame=CGRectMake(imageBG.XW_x, imageBG.XW_y, imageBG.XW_width, imageBG.XW_high);
    [self.tableView addSubview:BGView];

    _loginBtn = [XWCommonButton buttonWithType:UIButtonTypeCustom];
    
    [BGView addSubview:self.loginBtn];

    self.loginBtn.translatesAutoresizingMaskIntoConstraints = NO;
    [self.loginBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(100, 100));
        // 位置约束
        make.centerX.mas_equalTo(BGView.mas_centerX);
        make.centerY.mas_equalTo(BGView.mas_centerY);
    }];

    [self.loginBtn setImage:[UIImage imageNamed:@"user_deselected"] forState:UIControlStateNormal];
    [self.loginBtn addTarget:self action:@selector(loginBtnAction:) forControlEvents:(UIControlEventTouchUpInside)];
    [self.loginBtn setTitle:@"未登录" forState:UIControlStateNormal];




    
}
#pragma mark 登录按钮
-(void)loginBtnAction:(UIButton*)sender{
    XWLoginOrRegisterController *loginOrRegisterVc = [[XWLoginOrRegisterController alloc]init];
    [self presentViewController:loginOrRegisterVc animated:YES completion:nil];
}

-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    CGFloat yOffset  = scrollView.contentOffset.y;

    CGFloat alpha;
    CGFloat xOffset = (yOffset + BackGroupHeight)/2;
    if (yOffset < -BackGroupHeight) {
        CGRect rect = imageBG.frame;
        rect.origin.y = yOffset;
        rect.size.height =  -yOffset ;
        rect.origin.x = xOffset;
        rect.size.width = KScreen_Width + fabs(xOffset)*2;
        //头部视图背景图片随tableView拖拽的偏移量变大或缩小
        imageBG.frame = rect;
        
    }
    alpha = (yOffset+BackGroupHeight)/BackGroupHeight*10;
    alpha=fabs(alpha);
    alpha=fabs(1-alpha);
    alpha=alpha<0.2? 0:alpha;
    //实现BGViewtableView偏移量变化 改变灰度
    BGView.alpha=alpha;

}
@end
